package com.ruoyi.web.controller.okr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Bruce on 2021/9/11 20:41
 **/
@Controller
@RequestMapping("/star")
public class MyOkrStarController extends BaseController {

    @Autowired
    private IOkrStarService okrStarService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IOkrInfoService okrInfoService;

    @Autowired
    private IOkrCycleService okrCycleService;


    @Autowired
    private IOkrInformService okrInformService;

    String prefix = "okr";

    @RequestMapping("")
    public String getStars(Model model){
        List<OkrCycle> useCycleList = okrCycleService.selectOkrUseCycleList();
        model.addAttribute("cycleId", useCycleList.get(0).getId());
        return prefix + "/MyStars";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo queryStarList(SysUser user){
        startPage();
        List<UserStar> userStars = okrStarService.queryMyStarUsers(ShiroUtils.getUserId());
        List<SysUser> users = new ArrayList<>();
        List<SysUser> list = userService.selectUserList(user);
        for (UserStar star : userStars){
            users.add(userService.selectUserById(star.getStarUserId()));
        }
        List<SysUser> res = new ArrayList<>();
        for (SysUser u : users){
            for (SysUser searchUser : list){
                if (u.getUserId().equals(searchUser.getUserId())){
                    res.add(u);
                }
            }
        }
        return getDataTable(res);

    }

    @RequestMapping("/getOkrStars")
    public String getOkrStars(){
        return prefix + "/MyOkrStars";
    }

    @RequestMapping("/okrList")
    @ResponseBody
    public TableDataInfo queryStarOkrList(){
        List<OkrStar> okrStars = okrStarService.queryMyStarOkrs(ShiroUtils.getUserId());
        List<OkrInfo> okrInfos = new ArrayList<>();
        for (OkrStar star : okrStars){
            okrInfos.add(okrInfoService.selectOkrInfoById(star.getOkrId()));
        }
        return getDataTable(okrInfos);
    }

    @RequestMapping("/isStar")
    @ResponseBody
    public AjaxResult isStar(Long starUserId){
        UserStar userStar = new UserStar();
        userStar.setStarUserId(starUserId);
        userStar.setUserId(ShiroUtils.getUserId());
        int star = okrStarService.isStar(userStar);
        if (star > 0){
            return AjaxResult.success("已关注", star);
        }else{
            return AjaxResult.success("未关注", star);
        }
    }
    @PostMapping("/isOkrStar")
    @ResponseBody
    public AjaxResult isOkrStar(Long starOkrId){
        OkrStar okrStar = new OkrStar();
        okrStar.setUserId(ShiroUtils.getUserId());
        okrStar.setOkrId(starOkrId);
        int star = okrStarService.isOkrStar(okrStar);
        if (star > 0){
            return AjaxResult.success("已关注", star);
        }else{
            return AjaxResult.success("未关注", star);
        }
    }

    @RequestMapping("/starUser")
    @ResponseBody
    public AjaxResult starUser(Long starUserId){
        okrStarService.addStarUser(ShiroUtils.getUserId(), starUserId);

        return AjaxResult.success("关注成功");
    }

    @RequestMapping("/starOkr")
    @ResponseBody
    public AjaxResult starOkr(Long starOkrId){
        SysUser user = ShiroUtils.getSysUser();
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(starOkrId);
        okrStarService.addStarOkr(starOkrId, user.getUserId());

        if (!user.getUserId().equals(okrInfo.getUserId())){
            OkrInform okrInform = new OkrInform();
            okrInform.setType("关注");
            okrInform.setAvatar(user.getAvatar());
            okrInform.setInformTime(DateUtils.getNowDate());
            okrInform.setParentName(user.getUserName());
            okrInform.setUserId(okrInfo.getUserId());
            okrInform.setParentId(user.getUserId());
            okrInform.setContent(user.getUserName() + "关注了您的目标");
            okrInform.setUrl("okr/info/view/" + okrInfo.getId());
            okrInformService.insertOkrInform(okrInform);
        }
        return AjaxResult.success("关注成功");
    }

    @RequestMapping("/cancelStar")
    @ResponseBody
    public AjaxResult cancelStar(Long starUserId){
        UserStar userStar = new UserStar();
        userStar.setStarUserId(starUserId);
        userStar.setUserId(ShiroUtils.getUserId());
        okrStarService.cancelStarUser(userStar);
        return AjaxResult.success("取消关注");
    }

    @RequestMapping("/cancelStarOkr")
    @ResponseBody
    public AjaxResult cancelStarOkr(Long starOkrId){
        OkrStar okrStar = new OkrStar();
        okrStar.setUserId(ShiroUtils.getUserId());
        okrStar.setOkrId(starOkrId);
        okrStarService.cancelStarOkr(okrStar);
        SysUser user = ShiroUtils.getSysUser();
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(starOkrId);

        if (!user.getUserId().equals(okrInfo.getUserId())){
            OkrInform okrInform = new OkrInform();
            okrInform.setType("关注");
            okrInform.setAvatar(user.getAvatar());
            okrInform.setInformTime(DateUtils.getNowDate());
            okrInform.setParentName(user.getUserName());
            okrInform.setUserId(okrInfo.getUserId());
            okrInform.setParentId(user.getUserId());
            okrInform.setContent(user.getUserName() + "取消关注了您的目标");
            okrInform.setUrl("okr/info/view/" + okrInfo.getId());
            okrInformService.insertOkrInform(okrInform);
        }
        return AjaxResult.success("取消关注");
    }

    @RequestMapping("/getStarOkrList")
    @ResponseBody
    public List<OkrInfo> getStarOKrList(){
        List<OkrStar> okrStars = okrStarService.queryMyStarOkrs(ShiroUtils.getUserId());
        List<OkrInfo> okrInfos = new ArrayList<>();
        for (OkrStar star : okrStars){
            okrInfos.add(okrInfoService.selectOkrInfoById(star.getOkrId()));
        }
        return okrInfos;
    }
}
